﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Petaframe.DataAccess.HandmadeIntegrationTests
{
    public partial class OracleTestForm : Form
    {
        OracleDataService oracle;

        public OracleTestForm()
        {
            InitializeComponent();
        }

        private void OracleTestForm_Load(object sender, EventArgs e)
        {
            oracle = new OracleDataService("data source=XE;User ID=guney;Password=1");
        }

        private void btnFindBy_Click(object sender, EventArgs e)
        {
           // var result = oracle.FindBy<Category>();
            oracle.AddInputParameter(":p1", 1, DbType.Int32);
            var result = oracle.FindBy<Products2>(" where productid=:p1");

            toolStripStatusLabel1.Text = string.Format("{0}-{1}-{2}--{3}", result.ProductID, result.CategoryID, result.SupplierID,result.UnitPrice);
        }

        private void btnFindAll_Click(object sender, EventArgs e)
        {
            //var result = oracle.FindAll<Category>();
            oracle.AddInputParameter(":p1", 7, DbType.Int32);
            var result = oracle.FindAll<Category>("select * from categories where categoryID>:p1"); 

            dataGridView1.DataSource = result;
        }

        private void btnInsertItem_Click(object sender, EventArgs e)
        {
            //Category c = new Category() { CategoryName = "kaşık", Description = "kaşıklamaya yarar" };

            //var result = oracle.Insert(c);
            oracle.AddInputParameter(":p1", "elma", DbType.String);
            oracle.AddInputParameter(":p2", "elma suyu", DbType.String);
            var result = oracle.Insert("insert into categories(CategoryName,Description) values(:p1,:p2)", CommandType.Text);
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            oracle.AddInputParameter(":p1", 30, DbType.Int32);
            var result = oracle.FindBy<Category>(" where categoryID=:p1");

            result.CategoryName = "deneme";
            object result2 = oracle.Update(result);
        }

        private void btndelete_Click(object sender, EventArgs e)
        {
            oracle.AddInputParameter(":p1", 31, DbType.Int32);
            var result = oracle.FindBy<Category>(" where categoryID=:p1");
            var result2 = oracle.Delete(result);
        }

        int page = 1;
        private void btnPaging_Click(object sender, EventArgs e)
        {
            int size = 15;
           // dataGridView1.DataSource = oracle.Page<Products2>(page, size).items;
            PagedResults<Products2> result = oracle.Page<Products2>(page, size,"where unitprice>20");
            dataGridView1.DataSource = result.items;
            toolStripStatusLabel1.Text = String.Format("Page :{0} - total page={1} - total records :{2}", page, result.TotalPages, result.TotalRecords);
            page++;
        }

        DataTable dt;
        private void btnDetachedFindAll_Click(object sender, EventArgs e)
        {
            dt = oracle.DetachedQuery.FindAll("select * from products");
            //oracle.AddInputParameter(":p1", 20, DbType.Decimal);

            //dt = oracle.DetachedQuery.FindAll("select c.CategoryName,p.ProductName,p.UnitPrice from categories c join products p on c.categoryID=p.CategoryID where p.UnitPrice>:p1 order by p.UnitPrice");

            dataGridView1.DataSource = dt;
        }

        private void btnPersistTable_Click(object sender, EventArgs e)
        {
            dt.TableName = "Products";
            //var result = oracle.DetachedQuery.PersistTable(dt);

            oracle.BeginTransaction();
            var result = oracle.DetachedQuery.PersistTable(dt);
            oracle.CommitTransaction();
        }
        int index = 1;
        private void btnDetachedPaging_Click(object sender, EventArgs e)
        {
            oracle.AddInputParameter(":p1", 20, DbType.Decimal);

            dt = oracle.DetachedQuery.Page("select p.ProductID, c.categoryname,p.productname,p.unitprice from categories c join products p on c.categoryId=p.categoryId where p.unitprice>:p1 order by p.ProductID", index, 7);

            dataGridView1.DataSource = dt;
            index++;
        }
    }
}
